using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.TurnBase
{
    public class BattleUnit : IClass
    {
        void IClass.OnReset()
        {
            GameObject.Destroy(gameObject);
            for (int i = 0; i < m_components.Count; i++)
            {
                ClassManager.Free(m_components[i]);
            }
            m_components.Clear();
            ClassManager.Free(Data);
            Dispose();
        }
        
        protected virtual void Dispose()
        {
            
        }

        public UnitData Data;
        public GameObject gameObject;
        private List<IComponent> m_components;
        private RenderComponent renderComponent;
        private MoveComponent moveComponent;

        public virtual void Initialize(UnitData data,GameObject model = null)
        {
            this.Data = data;
            gameObject = new GameObject(data.id.ToString());
            gameObject.transform.SetParent(PveScene.pveRootTrans);
            gameObject.layer = LayerMask.NameToLayer("PveUnit");
            m_components = new List<IComponent>();
            
            AddComponent<ControlComponent>();
            AddComponent<InstructionComponent>();
            AddComponent<BattleComponennt>();
            AddComponent<HudComponent>();
            AddComponent<FloatComponent>();
            AddComponent<GravityComponent>();
            renderComponent = AddComponent<RenderComponent>();
            moveComponent = AddComponent<MoveComponent>();
            if (model != null)
            {
                renderComponent.ReceiveModel(model);
            }
            OnInitialize();
        }
        
        public void Birth()
        {
            Data.isDead = false;
            OnBirth();
        }
        
        private void OnBirth()
        {
            Data.birthPosition = TurnBaseManager.Instance.GetBirthPosition(Data.index,Data.team);
            Data.birthForward = TurnBaseManager.Instance.GetBirthForward(Data.team);
            for (int i = 0; i < m_components.Count; i++)
            {
                m_components[i].OnBirth();
            }
        }
        
        private void OnDead()
        {
            for (int i = 0; i < m_components.Count; i++)
            {
                m_components[i].OnDead();
            }
        }
        
        public virtual void Update()
        {
            for (int i = 0; i < m_components.Count; i++)
            {
                m_components[i].OnUpdate();
            }
        }
        
        public void Transmit(ETransmitType type, BaseTransmitArg arg = null)
        {
            for (int i = 0; i < m_components.Count; i++)
            {
                if (m_components[i] is ITransmit)
                {
                    ((ITransmit) m_components[i]).OnTransmit(type, arg);
                }
            }
        }
        
        protected T AddComponent<T>() where T : IComponent
        {
            var component = ClassManager.Get<T>();
            if (!m_components.Contains(component))
            {
                m_components.Add(component);
            }
            return component;
        }
        
        public void BeforeUpdate()
        {
            renderComponent?.BeforeUpdate();
        }

        public void LateUpdate()
        {
            moveComponent?.LateUpdate();
            renderComponent?.LateUpdate();
        }

        public void Play(string action,float speed = 1)
        {
            renderComponent?.Play(action);
            renderComponent?.SetAnimSpeed(speed);
        }
        
        public virtual void Dead()
        {
            if (Data.isDead)
            {
                return;
            }
            Data.isDead = true;
            OnDead();
            //PveManager.Instance.UnitDead(Data.id);
        }
        
        protected void OnInitialize()
        {
            for (int i = 0; i < m_components.Count; i++)
            {
                m_components[i].OnInitialize(this);
            }
        }
    }
}